package com.smile.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.smile.api.UserApi;
import com.smile.commons.page.R;
import com.smile.mapper.AlarmReceptionSettingMapper;
import com.smile.mapper.ReachTargetPointActionMapper;
import com.smile.model.domain.FeisuanAlarmReceptionSetting;
import com.smile.model.domain.FeisuanReachTargetPointAction;
import com.smile.model.dto.request.SetAlarmReceptionSettingRequestDTO;
import com.smile.model.response.response.GetAlarmReceptionSettingResponseDTO;
import com.smile.model.response.response.GetReachTargetPointActionResponseDTO;
import com.smile.model.request.GetUserByIdRequest;
import com.smile.model.response.GetUserByIdResponse;
import com.smile.service.AlarmReceptionSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AlarmReceptionSettingServiceImpl implements AlarmReceptionSettingService {

    @Autowired
    private AlarmReceptionSettingMapper alarmReceptionSettingMapper;

    @Autowired
    private ReachTargetPointActionMapper reachTargetPointActionMapper;

    @Autowired
    private UserApi userApi;

    @Override
    public Boolean setAlarmReceptionSetting(SetAlarmReceptionSettingRequestDTO setAlarmReceptionSettingRequestDTO) {

        Long userId = Long.valueOf(StpUtil.getLoginId().toString());
        GetUserByIdRequest getUserByIdRequest = new GetUserByIdRequest();
        getUserByIdRequest.setId(userId);
        R<GetUserByIdResponse> userById = userApi.getUserById(getUserByIdRequest);
        if (ObjectUtil.isNotNull(userById) || ObjectUtil.isNotNull(userById.getData())) {
            Long organizationId = userById.getData().getOrganizationId();
            QueryWrapper<FeisuanAlarmReceptionSetting> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("organization_id", organizationId);
            List<FeisuanAlarmReceptionSetting> feisuanAlarmReceptionSettings =
                    alarmReceptionSettingMapper.selectList(queryWrapper);
            if (CollectionUtil.isEmpty(feisuanAlarmReceptionSettings)) {
                FeisuanAlarmReceptionSetting feisuanAlarmReceptionSetting = BeanUtil.copyProperties(setAlarmReceptionSettingRequestDTO, FeisuanAlarmReceptionSetting.class);
                feisuanAlarmReceptionSetting.setOrganizationId(organizationId);
                alarmReceptionSettingMapper.insert(feisuanAlarmReceptionSetting);
                List<FeisuanReachTargetPointAction> feisuanReachTargetPointActions = BeanUtil.copyToList(setAlarmReceptionSettingRequestDTO.getAddReachTargetPointActions(), FeisuanReachTargetPointAction.class);
                List<FeisuanReachTargetPointAction> collect = feisuanReachTargetPointActions.stream().map(i -> {
                    FeisuanReachTargetPointAction i1 = i;
                    i1.setAlarmReceptionSettingId(feisuanAlarmReceptionSetting.getId());
                    return i1;
                }).collect(Collectors.toList());
                Db.saveBatch(collect);

            } else {
                UpdateWrapper<FeisuanAlarmReceptionSetting> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("organization_id", userById.getData().getOrganizationId());
                updateWrapper.set(ObjectUtil.isNotNull(setAlarmReceptionSettingRequestDTO.getDefaultFlyHeight()),
                        "default_fly_height", setAlarmReceptionSettingRequestDTO.getDefaultFlyHeight());
                updateWrapper.set(ObjectUtil.isNotNull(setAlarmReceptionSettingRequestDTO.getDefaultFlySpeed()),
                        "default_fly_speed", setAlarmReceptionSettingRequestDTO.getDefaultFlySpeed());
                updateWrapper.set(ObjectUtil.isNotNull(setAlarmReceptionSettingRequestDTO.getDefaultTurnBackHeight()),
                        "default_turn_back_height", setAlarmReceptionSettingRequestDTO.getDefaultTurnBackHeight());

                updateWrapper.set(ObjectUtil.isNotNull(setAlarmReceptionSettingRequestDTO.getAutoTreatTheCasesStatus()),
                        "auto_treat_the_cases_status", setAlarmReceptionSettingRequestDTO.getAutoTreatTheCasesStatus());

                updateWrapper.set(ObjectUtil.isNotNull(setAlarmReceptionSettingRequestDTO.getWarningInstanceExpire()),
                        "warning_instance_expire", setAlarmReceptionSettingRequestDTO.getWarningInstanceExpire());

                alarmReceptionSettingMapper.update(null, updateWrapper);

                QueryWrapper<FeisuanAlarmReceptionSetting> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("organization_id", userById.getData().getOrganizationId());
                alarmReceptionSettingMapper.selectList(queryWrapper1);


                UpdateWrapper<FeisuanReachTargetPointAction> updateWrapper2 = new UpdateWrapper<>();
                updateWrapper2.eq("alarm_reception_setting_id", feisuanAlarmReceptionSettings.get(0).getId());
                reachTargetPointActionMapper.delete(updateWrapper2);

                List<FeisuanReachTargetPointAction> feisuanReachTargetPointActions = BeanUtil.copyToList(setAlarmReceptionSettingRequestDTO.getAddReachTargetPointActions(), FeisuanReachTargetPointAction.class);
                List<FeisuanReachTargetPointAction> collect = feisuanReachTargetPointActions.stream().map(i -> {
                    FeisuanReachTargetPointAction i1 = i;
                    i1.setAlarmReceptionSettingId(feisuanAlarmReceptionSettings.get(0).getId());
                    return i1;
                }).collect(Collectors.toList());
                Db.saveBatch(collect);

            }
        }


        return Boolean.TRUE;
    }

    @Override
    public GetAlarmReceptionSettingResponseDTO getAlarmReceptionSetting() {

        Long userId = Long.valueOf(StpUtil.getLoginId().toString());
        GetUserByIdRequest getUserByIdRequest = new GetUserByIdRequest();
        getUserByIdRequest.setId(userId);
        R<GetUserByIdResponse> userById = userApi.getUserById(getUserByIdRequest);
        if (ObjectUtil.isNotNull(userById) || ObjectUtil.isNotNull(userById.getData())) {
            Long organizationId = userById.getData().getOrganizationId();
            QueryWrapper<FeisuanAlarmReceptionSetting> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("organization_id", organizationId);
            List<FeisuanAlarmReceptionSetting> feisuanAlarmReceptionSettings =
                    alarmReceptionSettingMapper.selectList(queryWrapper);
            if (CollectionUtil.isEmpty(feisuanAlarmReceptionSettings)) {
                //创建初始化数据
                FeisuanAlarmReceptionSetting feisuanAlarmReceptionSetting = new FeisuanAlarmReceptionSetting();
                feisuanAlarmReceptionSetting.setOrganizationId(organizationId);
                feisuanAlarmReceptionSetting.setAutoTreatTheCasesStatus(2);
                feisuanAlarmReceptionSetting.setDefaultFlySpeed(0L);
                feisuanAlarmReceptionSetting.setDefaultTurnBackHeight(0L);
                feisuanAlarmReceptionSetting.setDefaultFlyHeight(0L);
                feisuanAlarmReceptionSetting.setWarningInstanceExpire(0L);
                alarmReceptionSettingMapper.insert(feisuanAlarmReceptionSetting);
                return BeanUtil.copyProperties(feisuanAlarmReceptionSetting, GetAlarmReceptionSettingResponseDTO.class);
            } else {
                GetAlarmReceptionSettingResponseDTO getAlarmReceptionSettingResponseDTO = BeanUtil.copyProperties(feisuanAlarmReceptionSettings.get(0), GetAlarmReceptionSettingResponseDTO.class);

                QueryWrapper<FeisuanReachTargetPointAction> reachTargetPointActionQueryWrapper = new QueryWrapper<>();
                reachTargetPointActionQueryWrapper.eq("alarm_reception_setting_id", feisuanAlarmReceptionSettings.get(0).getId());
                List<FeisuanReachTargetPointAction> feisuanReachTargetPointActions = reachTargetPointActionMapper.selectList(reachTargetPointActionQueryWrapper);
                List<GetReachTargetPointActionResponseDTO> getReachTargetPointActionResponseDTOS = BeanUtil.copyToList(feisuanReachTargetPointActions, GetReachTargetPointActionResponseDTO.class);
                getAlarmReceptionSettingResponseDTO.setGetReachTargetPointActions(getReachTargetPointActionResponseDTOS);
                return getAlarmReceptionSettingResponseDTO;
            }
        } else {
            throw new RuntimeException("用户信息不存在");
        }
    }
}
